/*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */

package primitives;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import common.XmlConstants;

import SharedDrawing.WgContent;

public class P_Text extends Primitive 
{
	private JTextArea m_text;
	private Rectangle2D m_rect;
	private boolean isAdded;

	public void initPrimitive(int x, int y, int width, boolean fill, Color color, Color border)
	{
		try 
		{
			m_type = this.getClass().getSimpleName();
			initPrimitive(width, fill, color, border);
			
			isAdded = false;
			m_text = new JTextArea();
			m_rect = new Rectangle2D.Float(x, y, 0, 0);
		} 
		catch (Exception err) 
		{
			System.out.println("Erreur d'instanciation: " + err);
		}
	}	

	public void addToPanel(JPanel container)
	{
		isAdded = true;
		container.add(m_text);
	}
	
	public void removeToPanel(JPanel container)
	{
		isAdded = false;
		container.remove(m_text);
	}
	
	@Override
	public boolean isOverObject(int x, int y) 
	{
		if (m_rect.intersects(x - 3, y - 3, 6, 6)) 
		{
			return true;
		}
		return false;
	}
	
	@Override
	public void drawComponent(Graphics2D g) 
	{
		if(m_isFilled)
		{
			g.setColor(m_color);
			g.fill(m_rect);
		}
		
		g.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1f, new float[] {3f}, 0));
		g.setColor(Color.gray);
		g.draw(m_rect);
		
		g.setColor(m_borderColor);
		
		if(isAdded == false)
		{
			Font font = new Font("Arial", Font.PLAIN ,m_width * 10);
			g.setFont(font);
			g.setColor(m_borderColor);
			
			FontMetrics fontMetrics = g.getFontMetrics();
			int height = fontMetrics.getHeight();
			int decal = height;
	
			for(String str : splitLine(m_text.getText(), fontMetrics))
			{
				g.drawString(str, (int)m_rect.getX() + 7, (int)m_rect.getY() + 3 + decal);
				decal += height;
				if(decal > m_rect.getHeight()) break;
			}
		}
	}
	
	/**
	 * Function used to split a line depending of the bounding box dimensions
	 * @param text: the text to split
	 * @param fontMetrics: to get some information about the font of this text
	 * @return a list of String => the text splited in shorter lines 
	 */
	private ArrayList<String> splitLine(String text, FontMetrics fontMetrics) 
	{
		ArrayList<String> lines = new ArrayList<String>();
		
		int lineWidth = fontMetrics.stringWidth(text);
		float charWidth = (float)lineWidth / text.length();
		int nbCarMax = (int) (m_rect.getWidth() / charWidth) - 2;
		int lineLenght = text.length();
		
		if(nbCarMax < 0) nbCarMax = 0;
		
		while(lineLenght > nbCarMax)
		{
			lines.add(text.substring(0, nbCarMax));
			
			text = text.substring(nbCarMax);
			lineLenght = text.length();
		}
		
		lines.add(text.substring(0, lineLenght));
		
		return lines;
	}

	@Override
	public void setShape(int x, int y, int w, int h) 
	{
		m_text.setBounds(x + 6, y + 6, w - 11, h - 11);
		m_rect.setFrame(x, y, w, h);
	}
	
	@Override
	public WgContent toWgContent()
    {
		WgContent wg = new WgContent(m_type);
		
		wg.addContent(XmlConstants.primitiveId, String.valueOf(m_elementId));
		wg.addContent(XmlConstants.primitiveX, String.valueOf((int)m_rect.getX()));
        wg.addContent(XmlConstants.primitiveY, String.valueOf((int)m_rect.getY()));
        wg.addContent(XmlConstants.primitiveSizeX, String.valueOf((int)m_rect.getWidth()));
        wg.addContent(XmlConstants.primitiveSizeY, String.valueOf((int)m_rect.getHeight()));
		wg.addContent(XmlConstants.primitiveWidth, String.valueOf(m_width));
        wg.addContent(XmlConstants.primitiveFilled, new Boolean(m_isFilled).toString());  
		wg.addContent(XmlConstants.primitiveColor, Integer.toHexString(m_color.getRGB()));
        wg.addContent(XmlConstants.primitiveBorderColor, Integer.toHexString(m_borderColor.getRGB()));   
        wg.addContent(XmlConstants.primitiveText, m_text.getText());
        
        return wg;
    }
	
	@Override
	public void fromWgContent(WgContent wg)
    {
		m_rect.setFrame(Integer.parseInt(wg.get(XmlConstants.primitiveX)),
					    Integer.parseInt(wg.get(XmlConstants.primitiveY)),
					    Integer.parseInt(wg.get(XmlConstants.primitiveSizeX)),
					    Integer.parseInt(wg.get(XmlConstants.primitiveSizeY)));
		
		m_width = Integer.parseInt(wg.get(XmlConstants.primitiveWidth));
		m_isFilled = Boolean.parseBoolean(wg.get(XmlConstants.primitiveFilled));	
		m_color = Color.decode("0x" + wg.get(XmlConstants.primitiveColor).substring(2));
		m_borderColor = Color.decode("0x" + wg.get(XmlConstants.primitiveBorderColor).substring(2));   
        m_text.setText(wg.get(XmlConstants.primitiveText));
        
        isAdded = false;				  
    }

	@Override
	public Object getShape() {
		return m_rect;
	}
	
	public JTextArea getTextShape() {
		return m_text;
	}
}
